import axios from 'axios'
axios.defaults.baseURL = process.env.NODE_ENV == 'development' ? 'http://192.168.31.50:8080/api' : 'http://211.149.139.110:81/api'
axios.interceptors.request.use(config => {
    config.timeout = 10 * 10000
    config.withCredentials = true
    config.headers.token = `${localStorage.getItem('token')}`;
    return config
}, error => {
    return Promise.reject(error)
})
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            return Promise.resolve(response.data)
        } else if (response.data.code === 1401) {
            return Promise.reject(response.data)
        } else if (response.data.code >= 500) {
            return Promise.reject(response.data)
        } else {
            return Promise.reject(response.data)
        }
    },
    error => {
        if (error && error.response) {
            let res = {}
            res.code = error.response.status
            return Promise.reject(res)
        }
        return Promise.reject(error)
    }
)
export default function request(method, url, data) {
    method = method.toLocaleLowerCase()
    if (method === 'post') {
        return axios.post(url, data)
    } else if (method === 'get') {
        return axios.get(url, { params: data })
    }
}